﻿Imports System.Data.SqlClient


Public Class LoginHelper
    Private strUserName As String = ""
    Private strPassword As String = ""
    Private strMessage As String = ""
    Private bolIsAdmin As Boolean = False
    Private bolIsLoged As Boolean = False

    Public Property UserName() As String
        Get
            Return strUserName
        End Get
        Set(ByVal value As String)
            strUserName = value
        End Set
    End Property

    Public Property Password() As String
        Get
            Return strPassword
        End Get
        Set(ByVal value As String)
            strPassword = value
        End Set
    End Property

    Public Property Message() As String
        Get
            Return strMessage
        End Get
        Set(ByVal value As String)
            strMessage = value
        End Set
    End Property

    Public ReadOnly Property IsAdmin() As Boolean
        'set { bolIsAdmin = value; }
        Get
            Return bolIsAdmin
        End Get
    End Property

    Public ReadOnly Property IsLoged() As Boolean
        'set { bolIsLoged=value;}
        Get
            Return bolIsLoged
        End Get
    End Property

    Public Sub New(ByVal username As String, ByVal password As String)
        strUserName = username
        strPassword = password
        LogIn()
    End Sub

    Private Sub LogIn()
        Dim strCon As String = ConfigurationManager.ConnectionStrings("ConnectionStringOnline").ConnectionString()
        Dim myCon As New SqlConnection(strCon)
        myCon.Open()
        Dim myCommand As New SqlCommand()
        myCommand.CommandText = "dangnhap"
        myCommand.Parameters.Add("@taikhoan", UserName)
        myCommand.CommandType = CommandType.StoredProcedure
        myCommand.Connection = myCon
        Dim myAdapter As New SqlDataAdapter(myCommand)
        Dim myDataTable As New DataTable()
        myAdapter.Fill(myDataTable)

        If myDataTable.Rows.Count = 0 Then
            bolIsLoged = False
            strMessage = "Tài Khoản Không Đúng!"
        ElseIf myDataTable.Rows(0)(2).ToString() <> strPassword Then
            bolIsLoged = False
            strMessage = "Mật Khẩu Không Đúng!"
        Else
            bolIsLoged = True
            If myDataTable.Rows(0)(3).ToString() = "1" OrElse myDataTable.Rows(0)(2).ToString() = "2" Then
                bolIsAdmin = True
            End If
        End If
    End Sub
End Class
